// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.nio.ByteBuffer;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.IsoZombie;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.network.packets.INetworkPacket;
import zombie.vehicles.BaseVehicle;

public class VehicleHit extends Hit implements IMovable, INetworkPacket {
    public int vehicleDamage;
    public float vehicleSpeed;
    public boolean isVehicleHitFromBehind;
    public boolean isTargetHitFromBehind;

    public void set(
        boolean ignore,
        float damage,
        float hitForce,
        float hitDirectionX,
        float hitDirectionY,
        int _vehicleDamage,
        float _vehicleSpeed,
        boolean _isVehicleHitFromBehind,
        boolean _isTargetHitFromBehind
    ) {
        super.set(ignore, damage, hitForce, hitDirectionX, hitDirectionY);
        this.vehicleDamage = _vehicleDamage;
        this.vehicleSpeed = _vehicleSpeed;
        this.isVehicleHitFromBehind = _isVehicleHitFromBehind;
        this.isTargetHitFromBehind = _isTargetHitFromBehind;
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        super.parse(b, connection);
        this.vehicleDamage = b.getInt();
        this.vehicleSpeed = b.getFloat();
        this.isVehicleHitFromBehind = b.get() != 0;
        this.isTargetHitFromBehind = b.get() != 0;
    }

    @Override
    public void write(ByteBufferWriter b) {
        super.write(b);
        b.putInt(this.vehicleDamage);
        b.putFloat(this.vehicleSpeed);
        b.putBoolean(this.isVehicleHitFromBehind);
        b.putBoolean(this.isTargetHitFromBehind);
    }

    @Override
    public String getDescription() {
        return super.getDescription()
            + "\n\tVehicle [ speed="
            + this.vehicleSpeed
            + " | damage="
            + this.vehicleDamage
            + " | target-hit="
            + (this.isTargetHitFromBehind ? "FRONT" : "BEHIND")
            + " | vehicle-hit="
            + (this.isVehicleHitFromBehind ? "FRONT" : "REAR")
            + " ]";
    }

    void process(IsoGameCharacter isoGameCharacter0, IsoGameCharacter isoGameCharacter1, BaseVehicle baseVehicle) {
        super.process(isoGameCharacter0, isoGameCharacter1);
        if (GameServer.bServer) {
            if (this.vehicleDamage != 0) {
                if (this.isVehicleHitFromBehind) {
                    baseVehicle.addDamageFrontHitAChr(this.vehicleDamage);
                } else {
                    baseVehicle.addDamageRearHitAChr(this.vehicleDamage);
                }

                baseVehicle.transmitBlood();
            }
        } else if (GameClient.bClient) {
            if (isoGameCharacter1 instanceof IsoZombie) {
                ((IsoZombie)isoGameCharacter1).applyDamageFromVehicle(this.vehicleSpeed, this.damage);
            } else if (isoGameCharacter1 instanceof IsoPlayer) {
                ((IsoPlayer)isoGameCharacter1).getDamageFromHitByACar(this.vehicleSpeed);
                ((IsoPlayer)isoGameCharacter1).actionContext.reportEvent("washit");
                isoGameCharacter1.setVariable("hitpvp", false);
            }
        }
    }

    @Override
    public float getSpeed() {
        return this.vehicleSpeed;
    }

    @Override
    public boolean isVehicle() {
        return true;
    }
}
